<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>流浪猫狗收容系统</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <%@include file="../../common.jsp"%>
    <style>
        .layui-table-cell{
            height:80px;
            line-height: 80px;

        }
        .th.layui-table-col-special{
            height: 36px;
            line-height: 36px;
        }
        .layui-layer-title{
            background: #e0dbd6 !important;
        }

    </style>
</head>
<body style="background: #e0dbd6" >
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 5px -5px -5px -5px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item" style="align-content: center">
                        <div class="layui-inline" style="width: 20%;">
                            <label class="layui-form-label" style="width: 30%;">姓名</label>
                            <div class="layui-input-inline" style="width: 65%;">
                                <input type="text" name="name" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline" style="width: 20%;">
                            <label class="layui-form-label" style="width: 28%;">性别</label>
                            <div class="layui-input-inline" style="width: 65%;">
                                <select name="sex">
                                    <option value="" selected></option>
                                    <option value="1">公</option>
                                    <option value="2">母</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline" style="width: 20%;">
                            <label class="layui-form-label" style="width: 28%;">年龄</label>
                            <div class="layui-input-inline" style="width: 65%;">
                                <input type="number" name="age" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline" style="width: 25%;">
                            <label class="layui-form-label" style="width: 50%;">是否已被认养</label>
                            <div class="layui-input-inline" style="width: 45%; ">
                                <select name="isAdop">
                                    <option value="" selected></option>
                                    <option value="0">否</option>
                                    <option value="1">是</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline" style="width: 8%;">
                            <button type="submit" style="width: 100%" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>

                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter" style="text-align: center"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>

<script>
    layui.use(['form', 'table'], function () {

        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        table.render({
            elem: '#currentTableId',
            url: base_url + 'dog/getDogList.do',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: '6.5%',align: "center"},
                {field: 'id', title: '宠物编号', width: '8%',align: "center"},
                {field: 'name', title: '宠物名', width: '11%',align: "center"},
                {field: 'age', title: '年龄', width: '8%',align: "center"},
                {field: 'sex', title: '性别', width: '8%',align: "center",
                    templet:function (d) {
                        if(d.sex==1) return '公';
                        else if (d.sex==2) return '母';
                        else return '未知';
                    }},
                {field: 'isAdop', title: '是否已被认养', width: '11%',align: "center",event:'isAdop',
                    templet:function (d) {
                    if(d.isAdop==0) return '否';
                    else if (d.isAdop==1) return '是';
                    else return '未知';
                    }},

                {field: 'photo1', title: '图片一',templet:'<div><img src="{{ d.photo1}}" style="width: 194px; height: 140px"></div>',style:'',align: "center",event:'img1'},
                {field: 'photo2', title: '图片二',templet:'<div><img src="{{ d.photo2}}" style="width: 194px; height: 140px"></div>',style:'',align: "center",event:'img2'},
                {field: 'photo3', title: '图片三',templet:'<div><img src="{{ d.photo3}}" style="width: 194px; height: 140px"></div>',style:'',align: "center",event:'img3'},
                {field: 'photo4', title: '图片四',templet:'<div><img src="{{ d.photo4}}" style="width: 194px; height: 140px"></div>',style:'',align: "center",event:'img4'},
                // {field: 'tip', title: '备注'},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [5, 10, 20, 40, 50, 100],
            limit: 5,
            page: true,
            skin: 'line'
        });


        //监听图片点击事件
        // table.on('tool(currentTableFilter)', function (obj) {
        //     if(obj.event === 'isAdop'){
        //         alert("111")
        //     }
        // });


        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {

            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: data.field
            }, 'data');

            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加宠物',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '<c:url value="/dog/toAdd.do" />',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                // 批量删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;

                layer.confirm('确认批量删除？', function (index) {
                    $.ajax({
                        "url": base_url + "dog/deleteDogList.do",
                        headers: {
                            'Content-Type': 'application/json'
                        },
                        "data": JSON.stringify(data),
                        "method": "post",
                        success: function () {
                            layer.alert("批量删除成功",function () {
                                // 获得frame索引
                                var index = parent.layer.getFrameIndex(window.name);
                                //关闭当前frame
                                parent.layer.close(index);
                                window.parent.location.reload();
                            });
                        },
                        error: function () {
                            layer.alert("批量删除失败");
                        }
                    });
                });

            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            console.log(data);
            if (obj.event === 'edit') {
                var index = layer.open({
                    title: '编辑',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '<c:url value="/dog/toUpdate.do" />' + "?id=" + data.id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('确认删除？', function (index) {
                    // 删除
                    $.ajax({
                        "url": base_url + "dog/deleteDog.do",
                        "data": {
                            "id": data.id,
                            "page":data.page,
                        },
                        "method": "post",
                        success: function() {
                            layer.alert("删除成功！",function () {
                                // 获得frame索引
                                var index = parent.layer.getFrameIndex(window.name);
                                //关闭当前frame
                                parent.layer.close(index);
                                window.parent.location.reload();
                            })
                        },
                        error: function () {
                            layer.alert("删除失败！")
                        }
                    });
                });
            }
        });

    });
</script>

</body>
</html>